package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.DataSource;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.UserAreaCode;

import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyMessageVerifyCode;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyMobilePhoneNumber;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyPasswordSame;

/**
 * 找回密码
 */
public class RetrievePasswordViewModel extends AndroidViewModel {

    private MutableLiveData<Resource<BaseResponseModel>> mSendMsmLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mRetrievePasswordLiveData;

    public RetrievePasswordViewModel(@NonNull Application application) {
        super(application);
        mSendMsmLiveData = new MutableLiveData<>();
        mRetrievePasswordLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<BaseResponseModel>> sendMessage(String mobilePhone) {
        if (verifyMobilePhoneNumber(mobilePhone)) {
            mSendMsmLiveData.setValue(Resource.loading());
            getAreaCodeByMobilePhone(mobilePhone, region -> {
                DataRepository.get().sendMessage(region, mobilePhone, C.API_FORGET_PASSWORD_TYPE, result -> {
                    if (result.isSucceed()) {
                        mSendMsmLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
                    }
                });
            });
        }

        return mSendMsmLiveData;
    }

    private void getAreaCodeByMobilePhone(String mobilePhone, DataSource.DataCallback<String> callback) {
        DataRepository.get().getUserAreaByMobilePhone(mobilePhone, result -> {
            UserAreaCode.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                callback.onResult(resultData.getRegion());
            } else {
                mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
    }

    public MutableLiveData<Resource<BaseResponseModel>> retrievePassword(
            String mobilePhone, String password, String confirmPassword, String code) {

        if (verifyMobilePhoneNumber(mobilePhone) && verifyMessageVerifyCode(code)
                && verifyPasswordSame(password, confirmPassword)) {
            mRetrievePasswordLiveData.setValue(Resource.loading());
            DataRepository.get().retrievePassword(mobilePhone, password, code, result -> {
                if (result.isSucceed()) {
                    mRetrievePasswordLiveData.setValue(Resource.success(result.getMsg()));
                } else {
                    mRetrievePasswordLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mRetrievePasswordLiveData;
    }
}
